package com.boss.controller;

import com.alibaba.fastjson.JSON;
import com.boss.entity.AddressClassify;
import com.boss.service.AddressClassifyService;
import com.boss.util.TypeConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
//省市分类
@RestController
@RequestMapping("/addressClassify")
public class AddressClassifyController {
    @Autowired
    private AddressClassifyService addressClassifyService;
    //获取所有城市
    @RequestMapping("/getAllAddressClassify")
    public String getAllAddressClassify(@RequestParam(value = "parentId",required = false)String pid){
        Integer parentnum = null;//父级编号
        if(pid != null && !"".equals(pid)) {//判断编号是否合法
            parentnum = Integer.parseInt(pid);
        }
        List<AddressClassify> list =  addressClassifyService.getAllAddressClassify1(parentnum);
        String json =  JSON.toJSONString(list);
        System.out.println(json);
        return json;
    }

    //根据城市id获取城市
    @RequestMapping("/getAddressClassifyById")
    public String getAddressClassifyById(@RequestParam(value = "addressClassifyId1",required = false)String addressClassifyId1){
        AddressClassify  adcc= addressClassifyService.getAddressClassifyById(TypeConversion.Objects(addressClassifyId1));
        if(adcc.getParentId() != null){//判断父级是否为空
            AddressClassify  adcctest = addressClassifyService.getAddressClassifyById(adcc.getParentId());//临时存储对象
             adcc.setAddressClassifyName1(adcctest.getAddressClassifyName());//获得二级名称
             if(adcctest.getParentId() != null){//判断二级是否有父类
                 adcc.setAddressClassifyName2(adcctest.getAddressClassifyName());//获得三级名称
             }
        }
        System.out.println(adcc.getAddressClassifyName()+"=="+adcc.getAddressClassifyName1()+"=="+adcc.getAddressClassifyName2());
        String json = JSON.toJSONString(adcc);
        System.out.println(json);
        return json;
    }
}
